package com.company.project.redis;

/**
 * Redis Key构建器
 *
 * @author chenyang
 * @date 2016/7/24
 */
public class KeyBuilder {
    private static final String NAMESPACE = "project:";

    /**
     * 生成Redis Key字符串
     *
     * @param module 模块或子系统标识
     * @param key 键名
     * @param ids 关系主键ID集合
     * @return redis键字符串
     *
     * @author chenx
     * @date 2016/4/21
     */
    public static String generateKey(final String module, final String key, final long... ids) {
        StringBuilder result = new StringBuilder(NAMESPACE).append(module).append(":").append(key);
        for (long id : ids) {
            result.append(":").append(id);
        }
        return result.toString();
    }

    /**
     * 生成Redis Key字符串
     *
     * @param key 键名
     * @param others 其他因素
     * @return redis键字符串
     *
     * @author chenyang
     * @date 2017/07/24
     */
    public static String generateKeyByStrs(final String key, final String... others) {
        StringBuilder result = new StringBuilder(NAMESPACE).append(key);
        for (String param : others) {
            result.append(":").append(param);
        }

        return result.toString();
    }

    /**
     * 生成Redis Key字符串
     *
     * @param key 标识字符
     * @param ids 关系主键ID集合
     * @return redis键字符串
     *
     * @author chenyang
     * @date 2015/8/15
     */
    public static String generateKey(final String key, final long... ids) {
        StringBuilder result = new StringBuilder(NAMESPACE).append(key);
        for (long id : ids) {
            result.append(":").append(id);
        }
        return result.toString();
    }

    /**
     * 生成Redis Key字符串
     *
     * @param key 标识字符
     * @return redis键字符串
     *
     * @author chenyang
     * @date 2017/7/24
     */
    public static String generateKey(final String key) {
        return NAMESPACE + key;
    }
}
